package com.custom.action.joiner;


import com.custom.action.interfaces.ColumnParseHandler;
import com.custom.action.joiner.interfaces.OnExpression;
import com.custom.comm.enums.DbJoinStyle;
import com.custom.comm.utils.StrUtils;

import java.util.Map;

/**
 * @author Xiao-Bai
 * @since 2023/4/30 12:00
 */

public class LambdaJoinConditionWrapper<T> extends AbstractJoinConditionWrapper<T, LambdaJoinConditionWrapper<T>> {


    public LambdaJoinConditionWrapper(Class<T> entityClass) {
        wrapperInitialize(entityClass);
    }

    public LambdaJoinConditionWrapper(Class<T> entityClass, String alias) {
        wrapperInitialize(entityClass, alias);
    }

    protected LambdaJoinConditionWrapper(Class<T> entityClass, String alias, Map<Class<?>, JoinTableCache> joinTableCacheMap,
                                         Map<Class<?>, ColumnParseHandler> columnParseCacheMap, Class<?> joinClass) {
        wrapperInitialize(entityClass, alias, joinTableCacheMap, columnParseCacheMap, joinClass);
    }

    @Override
    protected LambdaJoinConditionWrapper<T> getInstance() {
        return new LambdaJoinConditionWrapper<>(
                getEntityClass(),
                getTableAlias(),
                getJoinTableCacheMap(),
                getColumnParseCacheMap(),
                null
        );
    }


    protected <Any> LambdaJoinConditionWrapper<T> getInstance(Class<Any> joinClass, String joinAlias) {
        return new LambdaJoinConditionWrapper<>(
                getEntityClass(),
                StrUtils.isBlank(joinAlias) ? getTableAlias() : joinAlias,
                getJoinTableCacheMap(),
                getColumnParseCacheMap(),
                joinClass
        );
    }


    @Override
    public <A> LambdaJoinConditionWrapper<T> join(boolean condition, DbJoinStyle joinStyle, Class<A> joinClass, String joinAlias, OnExpression expression) {
        if (condition) {
            LambdaJoinConditionWrapper<T> instance = getInstance(joinClass, joinAlias);
            expression.accept(instance);
            initJoinTable(joinStyle, instance);
        }
        return childrenClass;
    }


}
